<template>
  <div class="register-area" data-info="建档申领">
    <el-button class="next" @click="handleNext" type="primary">
      下一步{{ stepInfo }}
    </el-button>

    <div class="step-comps-area" data-info="具体的业务">
      <component
        :is="curCompName"
        @handleSure="handleSure"
        @sureBtn="sureBtn"
        @cancelBtn="cancelBtn"
        @handlePatientInfoSure="handlePatientInfoSure"
        @QueryTimeSingleSure="QueryTimeSingleSure"
        comTitle="请输入住院号码"
        inputTitle="住院号码："
      />
    </div>
    <!--底部操作-->
    <PatientNumberOperate
      @handleBack="handlePre"
      :key="widget"
      :time="120000"
    />
  </div>
</template>

<script>
import PatientNumberOperate from "../comps/patientNumberOperate";
import AddMobileNumber from "@sc/DRCbiz/comps/Add-mobile-number/index.vue";
import InpatientInformation from "@sc/DRCbiz/comps/inpatient-information/index.vue";
import QueryTimeSingle from "@sc/DRCbiz/comps/Query-time-single/index.vue";
import OneDayHospitalizationList from "@sc/DRCbiz/comps/One-day-hospitalization-list/index.vue";
import { v4 as uuid } from "uuid";

import { AndroidToWeb, buildParentApi } from "@/utils/designer/util";
let buildParent = buildParentApi();
export default {
  name: "FileApply",

  componentName: "FileApply",

  components: {
    PatientNumberOperate,
    AddMobileNumber,
    InpatientInformation,
    QueryTimeSingle,
    OneDayHospitalizationList
  },

  data() {
    return {
      widget: uuid(),
      steps: 1,
      curCompName: "AddMobileNumber",
      compsOptions: {
        1: "AddMobileNumber",
        2: "InpatientInformation",
        3: "QueryTimeSingle",
        4: "OneDayHospitalizationList"
      },
    };
  },

  computed: {
    shownOperate() {
      return this.steps !== 1;
    },
    patientInfo() {
      return this.$store.getters["DRCbiz/getPatientInfo"];
    },
    // 步骤
    stepInfo() {
      return this.$store.getters["DRCbiz/getStepsInfo"];
    },
    //选中的卡片信息
    cardTypeInfo() {
      return this.$store.getters["DRCbiz/getCardTypeInfo"];
    },
  },

  mounted() {
    this.$store.dispatch("DRCbiz/setCTDTimeInfo", 1200000);
  },

  methods: {
    /**
     * @name Query-time-single选择时间
     */
    QueryTimeSingleSure(time) {
      let devInfo = JSON.parse(localStorage.getItem("deviceInfo"));
      let inpatientMsg = this.$store.getters["DRCbiz/getInpatientInformation"]
      let ZYQueryBillDetailInfoParams = {
        Request: {
          deviceIp: devInfo.screenIp,
          flowId: uuid(),
          timestamp: this.$day().valueOf(),
          terminalNo: devInfo?.screenId, // 自助机设备终端	C	可空	根据医院实际情况
          method: "ZYQueryBillDetail",
          patientHosId: inpatientMsg.patientHosId, // 住院号 是否为空根据医院情况
          cardNo: "", // 卡号
          cardType: "", // 卡类型
          startDate: time, // 开始时间 "2025-02-21"
          endDate: time, // 结束时间 "2025-02-21"
          birthday: inpatientMsg.birthday, // 生日 "1969-06-19"
        },
      };

      let ZYQueryBillDetailQueryParams = this.$x2js.js2xml(
        ZYQueryBillDetailInfoParams
      );
      buildParent.executeParentMethod(
        "axiosRep",
        ZYQueryBillDetailQueryParams,
        "ZYQueryBillDetail"
      );
    },
    handlePatientInfoSure() {
      let stepInfo = parseInt(this.stepInfo);
      this.$store.dispatch("DRCbiz/setStepsInfo", ++stepInfo);
    },
    /**
     * @name 住院号码-确定
     */
    sureBtn(value) {
      // this.steps = ++this.steps;
      // let stepInfo = parseInt(this.stepInfo);
      // this.$store.dispatch("DRCbiz/setStepsInfo", ++stepInfo);
      let devInfo = JSON.parse(localStorage.getItem("deviceInfo"));
      let ZYQueryPatientInfoInfoParams = {
        Request: {
          deviceIp: devInfo.screenIp,
          flowId: uuid(),
          timestamp: this.$day().valueOf(),
          terminalNo: devInfo?.screenId, // 自助机设备终端	C	可空	根据医院实际情况
          method: "ZYQueryPatientInfo",
          patientId: value, // 住院号 是否为空根据医院情况 00210277
          // cardNo: this.patientInfo.cardNo, // 卡号
          // cardType: this.patientInfo.cardType, // 卡类型
          patientName: "", // 开始时间
        },
      };
      let ZYQueryPatientInfoParams = this.$x2js.js2xml(
        ZYQueryPatientInfoInfoParams
      );
      buildParent.executeParentMethod(
        "axiosRep",
        ZYQueryPatientInfoParams,
        "ZYQueryPatientInfo"
      );
    },
    /**
     * @name 手机号取消
     */
    cancelBtn() {
      // this.steps = --this.steps;
      let stepInfo = parseInt(this.stepInfo);
      this.$store.dispatch("DRCbiz/setStepsInfo", ++stepInfo);
    },

    handleSure(useMsg) {
      console.log("sure1", useMsg, this.patientInfo);
      // <Request>
      //   <deviceIp>172.17.43.202</deviceIp>
      //   <flowId>JWS30320250206081219385</flowId>
      //   <timestamp>20250206081219385</timestamp>
      //   <terminalNo>JWS303</terminalNo>
      //   <method>QueryCostSummary</method>
      //   <patientId>4026835</patientId>
      // </Request>
      const { patientId } = this.patientInfo;
      let params = {
        Request: {
          deviceIp: "",
          flowId: "",
          timestamp: "",
          terminalNo: "",
          method: "QueryCostSummary",
          patientId,
        },
      };
      let paramsInfo = this.$x2js.js2xml(params);
      buildParent.executeParentMethod(
        "axiosRep",
        paramsInfo,
        "QueryCostSummary"
      );
    },
    async handleNext() {
      let stepInfo = parseInt(this.stepInfo);
      this.widget = uuid();
      if (stepInfo === 2) {
        await this.$store.dispatch("DRCbiz/setStepsInfo", 1);
      } else {
        await this.$store.dispatch("DRCbiz/setStepsInfo", ++stepInfo);
      }
    },

    // 返回上一层&首页
    // handlePre() {
    //   clearInterval(this.timer);
    //   if (this.steps === 1) {
    //     this.$router.push({
    //       path: `/DRCbizList`,
    //     });
    //   } else {
    //     this.steps = --this.steps;
    //   }
    // },
    // 返回上一层&首页
    async handlePre() {
      this.widget = uuid();
      let stepInfo = parseInt(this.stepInfo);
      if (stepInfo === 1) {
        await this.$router.push({
          path: `/DRCbizList`,
        });
      } else {
        await this.$store.dispatch("DRCbiz/setStepsInfo", --stepInfo);
      }
      switch (parseInt(this.stepInfo)) {
        case 2:
          let cardTypeInfo = this.cardTypeInfo;
          Reflect.set(cardTypeInfo, "cardNo", null);
          await this.$store.dispatch("DRCbiz/setCardTypeInfo", cardTypeInfo);
          buildParent.executeParentMethod("openScan");
          break;
      }
    },
  },

  watch: {
    // 步距
    stepInfo: {
      handler: function (val) {
        const self = this;
        self.widget = uuid();
        let OPTypeInfo = self.OPTypeInfo;
        let cardTypeInfo = self.cardTypeInfo;
        if (cardTypeInfo && val === 3) {
          // 电子医保特殊处理
          if (parseInt(cardTypeInfo?.id) === 29) {
            buildParent.executeParentMethod("MICode");
          } else {
            buildParent.executeParentMethod("openScan");
          }
        } else {
          buildParent.executeParentMethod("closeScan");
        }

        self.curCompName = Reflect.get(self.compsOptions, val);
      },
      deep: true,
      immediate: true,
    },
  },
};
</script>

<style scoped lang="scss">
@import "./assets/style";
</style>